<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <title>用户信息管理</title>
    <style>
        body {
            overflow-y: scroll;
            /* 确保滚动条始终可见 */
            -ms-overflow-style: scrollbar;
            scrollbar-width: thin;
            scrollbar-color: #888 #f0f0f0;
        }

        .pagination {
            display: flex;
            justify-content: center;
            align-items: center;
            list-style: none;
            padding: 0;
            margin: 0;
            gap: 10px; /* 为列表项之间添加间距 */
        }

        .pagination li {
            margin: 0;
            white-space: nowrap; /* 防止文本换行 */
        }

        .pagination a {
            display: inline-block;
            padding: 8px 12px;
            background-color: #f4f4f4;
            color: #333;
            text-decoration: none;
            border-radius: 4px;
        }

        .pagination a:hover {
            background-color: #e0e0e0;
        }

        .pagination .active {
            background-color: #007bff;
            color: white;
        }

        .pagination .disabled {
            background-color: #ccc;
            color: #666;
            cursor: not-allowed;
        }

        /* 当当前页数等于总页数且等于 1 时，隐藏上一页和下一页按钮内容 */
        {% if page == total_pages and total_pages == 1 %}
        .pagination li:first-child a,
        .pagination li:last-child a {
            visibility: hidden;
        }
        {% endif %}

        /* 当当前页数等于总页数且大于 1 时，隐藏下一页按钮内容 */
        {% if page == total_pages and total_pages > 1 %}
        .pagination li:last-child a {
            visibility: hidden;
        }
        {% endif %}

        /* 当当前页数为 1 时，隐藏上一页按钮内容 */
        {% if page == 1 %}
        .pagination li:first-child a {
            visibility: hidden;
        }
        {% endif %}

        /* 模态框样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0,0,0,0.4);
        }

        .modal-content {
            background-color: #fefefe;
            margin: 10% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
            max-width: 500px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.2);
            animation-name: animatetop;
            animation-duration: 0.4s;
        }

        @keyframes animatetop {
            from {top: -300px; opacity: 0}
            to {top: 0; opacity: 1}
        }

        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }

        .close:hover,
        .close:focus {
            color: black;
            text-decoration: none;
            cursor: pointer;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        .form-group input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }

        .error-message {
            color: red;
            font-size: 0.9em;
            margin-top: 5px;
        }
    </style>
    <script>
        // 模态框控制
        function openModal() {
            document.getElementById('addUserModal').style.display = 'block';
        }

        function closeModal() {
            document.getElementById('addUserModal').style.display = 'none';
            clearForm();
        }

        // 清空表单
        function clearForm() {
            document.getElementById('username').value = '';
            document.getElementById('realname').value = '';
            document.getElementById('idcard').value = '';
            document.getElementById('phone').value = '';
            document.querySelectorAll('.error-message').forEach(el => el.textContent = '');
        }

        // 表单验证
        function validateForm() {
            let isValid = true;
            const username = document.getElementById('username').value;
            const realname = document.getElementById('realname').value;
            const idcard = document.getElementById('idcard').value;
            const phone = document.getElementById('phone').value;

            // 简单验证
            if (username.trim() === '') {
                document.getElementById('usernameError').textContent = '账号不能为空';
                isValid = false;
            } else {
                document.getElementById('usernameError').textContent = '';
            }

            if (realname.trim() === '') {
                document.getElementById('realnameError').textContent = '姓名不能为空';
                isValid = false;
            } else {
                document.getElementById('realnameError').textContent = '';
            }

            if (idcard.trim() === '') {
                document.getElementById('idcardError').textContent = '身份证号不能为空';
                isValid = false;
            } else if (!/^\d{17}[\dXx]$/.test(idcard)) {
                document.getElementById('idcardError').textContent = '身份证号格式不正确';
                isValid = false;
            } else {
                document.getElementById('idcardError').textContent = '';
            }
             if (password.trim() === '') {
                document.getElementById('passwordError').textContent = '密码不能为空';
                isValid = false;
            } else if (password.length < 3) {
                document.getElementById('passwordError').textContent = '密码长度至少为3位';
                isValid = false;
            } else {
                document.getElementById('passwordError').textContent = '';
            }

            if (phone.trim() === '') {
                document.getElementById('phoneError').textContent = '手机号不能为空';
                isValid = false;
            } else if (!/^1[3-9]\d{9}$/.test(phone)) {
                document.getElementById('phoneError').textContent = '手机号格式不正确';
                isValid = false;
            } else {
                document.getElementById('phoneError').textContent = '';
            }

            if (isValid) {
                // 提交表单
                document.getElementById('addUserForm').submit();
            }

            return false; // 阻止表单默认提交
        }

        // 点击模态框外部关闭
        window.onclick = function(event) {
            const modal = document.getElementById('addUserModal');
            if (event.target === modal) {
                closeModal();
            }
        }

        // 删除用户确认
        function deleteUser(username) {
            if (confirm("确定要删除该用户吗？")) {
                window.location.href = "/delete_user/" + username;
            }
        }
    </script>
</head>

<body class="bg-gray-100">
    <!-- 导航栏 -->
    <nav class="bg-blue-600 text-white p-4 h-16">
        <div class="container mx-auto flex justify-between items-center">
            <h1 class="text-2xl font-bold">用户信息管理</h1>
            <ul class="flex space-x-4">
                <li><a href="{{ url_for('history') }}" class="hover:text-gray-300">病例管理</a></li>
                <li><a href="{{ url_for('user_management') }}" class="hover:text-gray-300">用户管理</a></li>
                <li><a href="{{ url_for('logout') }}" class="hover:text-gray-300">退出登录</a></li>
            </ul>
        </div>
    </nav>
    <!-- 用户信息表格 -->
    <div class="container mx-auto py-8 px-4">
        <div class="bg-white p-8 rounded-lg shadow-md max-w-full mx-auto">
            <div class="flex justify-between items-center mb-6">
                <h1 class="text-3xl font-bold text-gray-800">用户信息管理</h1>
                <button class="bg-green-500 hover:bg-green-600 text-white px-4 py-2 rounded-lg transition-all duration-300 transform hover:scale-105" onclick="openModal()">
                    <i class="fa fa-plus mr-2"></i>添加用户
                </button>
            </div>

            <table class="table-auto w-full">
                <thead>
                    <tr>
                        <th class="px-4 py-2 text-left">账号</th>
                        <th class="px-4 py-2 text-left">用户姓名</th>
                        <th class="px-4 py-2 text-left">身份证号</th>
                        <th class="px-4 py-2 text-left">手机号</th>
                        <th class="px-4 py-2 text-left">操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for user in users %}
                    <tr class="hover:bg-gray-50 transition-colors duration-200">
                        <td class="border px-4 py-2">{{ user.username }}</td>
                        <td class="border px-4 py-2">{{ user.realname }}</td>
                        <td class="border px-4 py-2">{{ user.idcard }}</td>
                        <td class="border px-4 py-2">{{ user.phone }}</td>
                        <td class="border px-4 py-2">
                            <button class="bg-red-500 hover:bg-red-600 text-white px-3 py-1.5 rounded transition-all duration-200 transform hover:scale-105" onclick="deleteUser('{{ user.username }}')">
                                <i class="fa fa-trash mr-1"></i>删除
                            </button>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
            <!-- 分页按钮 -->
            <ul class="pagination mt-8">
                <li>
                    <a href="{{ url_for('user_management', page=page - 1) }}"
                       class="{% if page == 1 %}disabled{% endif %}">
                        &laquo; 上一页
                    </a>
                </li>
                <li><span class="text-gray-700">{{ page }}/{{ total_pages }}</span></li>
                <li>
                    <a href="{{ url_for('user_management', page=page + 1) }}"
                       class="{% if page == total_pages %}disabled{% endif %}">
                        下一页 &raquo;
                    </a>
                </li>
            </ul>
        </div>
    </div>

    <!-- 添加用户模态框 -->
    <div id="addUserModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="closeModal()">&times;</span>
            <h2 class="text-xl font-bold mb-4">添加用户</h2>
            <form id="addUserForm" action="{{ url_for('add_user') }}" method="post" onsubmit="return validateForm()">
                <div class="form-group">
                    <label for="username">账号：</label>
                    <input type="text" id="username" name="username" title="请输入 8 位整数" required>
                    <span id="usernameError" class="error-message"></span>
                </div>
                <div class="form-group">
                    <label for="realname">姓名：</label>
                    <input type="text" id="realname" name="realname" required>
                    <span id="realnameError" class="error-message"></span>
                </div>
                <div class="form-group">
                    <label for="idcard">身份证号：</label>
                    <input type="text" id="idcard" name="idcard" required>
                    <span id="idcardError" class="error-message"></span>
                </div>
                <div class="form-group">
                    <label for="password">密码：</label>
                    <input type="password" id="password" name="password" required>
                    <span id="passwordError" class="error-message"></span>
                </div>
                <div class="form-group">
                    <label for="phone">手机号：</label>
                    <input type="text" id="phone" name="phone" required>
                    <span id="phoneError" class="error-message"></span>
                </div>
                <div class="flex justify-end space-x-3 mt-6">
                    <button type="button" class="bg-gray-300 hover:bg-gray-400 text-gray-800 px-4 py-2 rounded transition-all duration-200" onclick="closeModal()">取消</button>
                    <button type="submit" class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded transition-all duration-200 transform hover:scale-105">
                        <i class="fa fa-save mr-1"></i>保存
                    </button>
                </div>
            </form>
        </div>
    </div>
</body>

</html>